package org.apache.commons.graph.algorithms.connectivity.internal

import org.apache.commons.graph.Graph
import org.apache.commons.graph.algorithms.visit.BaseGraphVisitHandler
import org.apache.commons.graph.algorithms.visit.VisitState

internal class ConnectedComponentHandler<V, E>(
    private val untouchedVertices: MutableList<V>
) : BaseGraphVisitHandler<V, E, Graph<V, E>, List<V>>() {

    private val touchedVertices: MutableList<V> = mutableListOf()

    /**
     * {@inheritDoc}
     */
    override fun finishVertex(vertex: V): VisitState {
        untouchedVertices.remove(vertex)
        touchedVertices.add(vertex)
        return VisitState.CONTINUE
    }

    /**
     * {@inheritDoc}
     */
    override fun onCompleted(): List<V> {
        return touchedVertices
    }
}